package com.lhkj.ct.meta.modules.patient.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lhkj.ct.meta.modules.patient.model.entity.TblPatTag;
import com.lhkj.ct.meta.modules.patient.mapper.PatTagMapper;
import com.lhkj.ct.meta.modules.patient.service.PatTagService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 档案标签 服务实现类
 * </p>
 *
 * @author jobob
 * @since 2023-09-27
 */
@Service
public class PatTagServiceImpl extends ServiceImpl<PatTagMapper, TblPatTag> implements PatTagService {


    @Override
    public List<String> getByPatId(String userId) {
        return getBaseMapper().getByPatId(userId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(String userId, List<String> tagIds) {
        deleteByUser(userId);
        if (!tagIds.isEmpty()) {
            tagIds.stream()
                    .map(id -> new TblPatTag(userId, id))
                    .forEach(
                            item -> getBaseMapper().insert(item)
                    );
        }
    }

    @Override
    public void deleteByUser(String userId) {
        getBaseMapper().delete(new LambdaQueryWrapper<TblPatTag>().eq(TblPatTag::getPatId, userId));
    }
}
